package vn.personal.anhht.bloback12.activity;

import vn.personal.anhht.bloback12.util.Constants;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;

public class TabSettingSmsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{

    Resources rs;
    CheckBoxPreference cb;
    ListPreference listPref;
    ListPreference listPref1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPreferenceManager().setSharedPreferencesName(Constants.SETTINGS_SHARED_PREFERENCES_NAME);
        rs = getResources();
        addPreferencesFromResource(R.xml.preferences_sms);
        initSettings();
        
    }

    private void initSettings() {
        cb = (CheckBoxPreference) findPreference(rs.getString(R.string.sms_pref_turn_key));
        listPref = (ListPreference) findPreference(rs.getString(R.string.sms_pref_mode_block_key));
        listPref1 = (ListPreference) findPreference(rs.getString(R.string.sms_pref_mode_inform_key));      
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
        Preference pref = findPreference(key);
        if (pref instanceof CheckBoxPreference) {
            CheckBoxPreference cbp = (CheckBoxPreference) pref;
            if (cbp.isChecked()) {
                pref.setSummary(rs.getString(R.string.sms_pref_turn_summary_1));
            } else {
                pref.setSummary(rs.getString(R.string.sms_pref_turn_summary));
            }
            
        }
        
        if(pref instanceof ListPreference){
            ListPreference lp = (ListPreference) pref;
            if((rs.getString(R.string.sms_pref_mode_block_key)).equals(key)){
                String[] arrayModeLockValue = rs.getStringArray(R.array.entries_values_list_lockmode_sms);
                String[] arrayModeLock = rs.getStringArray(R.array.entries_list_lockmode_sms);
                if(lp.getValue().equals(arrayModeLockValue[0])){
                    lp.setSummary(arrayModeLock[0]);
                }else if (lp.getValue().equals(arrayModeLockValue[1])){
                    lp.setSummary(arrayModeLock[1]);
                }else if (lp.getValue().equals(arrayModeLockValue[2])){
                    lp.setSummary(arrayModeLock[2]);
                }else{
                    lp.setSummary(arrayModeLock[3]);
                }

            }
            
            if((rs.getString(R.string.sms_pref_mode_inform_key)).equals(key)){
                String[] arrayModeInformValue = rs.getStringArray(R.array.entries_values_list_inform_sms);
                String[] arrayModeInform = rs.getStringArray(R.array.entries_list_inform_sms);
                if(lp.getValue().equals(arrayModeInformValue[0])){
                    lp.setSummary(arrayModeInform[0]);
                }else{
                    lp.setSummary(arrayModeInform[1]);
                }
            }
        }
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        if(cb.isChecked()){
            cb.setSummary(rs.getString(R.string.sms_pref_turn_summary_1));
        }
        if(!cb.isChecked()){
            cb.setSummary(rs.getString(R.string.sms_pref_turn_summary));
        }
        String[] arrayModeLockValue = rs.getStringArray(R.array.entries_values_list_lockmode_sms);
        String[] arrayModeLock = rs.getStringArray(R.array.entries_list_lockmode_sms);
        if(listPref.getValue() != null){
            if(listPref.getValue().equals(arrayModeLockValue[0])){
                listPref.setSummary(arrayModeLock[0]);
            }else if(listPref.getValue().equals(arrayModeLockValue[1])){
                listPref.setSummary(arrayModeLock[1]);
            }else if(listPref.getValue().equals(arrayModeLockValue[2])){
                listPref.setSummary(arrayModeLock[2]);
            }else{
                listPref.setSummary(arrayModeLock[3]);
            }
        }
        String[] arrayModeInformValue = rs.getStringArray(R.array.entries_values_list_inform_sms);
        String[] arrayModeInform = rs.getStringArray(R.array.entries_list_inform_sms);
        if(listPref1.getValue().equals(arrayModeInformValue[0])){
            listPref1.setSummary(arrayModeInform[0]);
        }else{
            listPref1.setSummary(arrayModeInform[1]);
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }
    
    
}
